/**
 * @(#)ProcessInstanceImageTag.java Feb 1, 2010
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.web.taglibs;

import java.util.Set;

import javax.servlet.jsp.JspWriter;

import org.jbpm.api.model.ActivityCoordinates;

import com.wilmar.process.core.services.api.ISystemHistoryProcessInstanceService;
import com.wilmar.process.core.services.api.ISystemProcessInstanceService;
import com.wilmar.process.enumerate.EnumProcessBeanIdSet;
import com.wilmar.process.enumerate.EnumProcessSystemTagFixedVariableNameSet;
import com.wilmar.process.exception.ObjectNotFoundException;
import com.wilmar.process.system.ProcessSystem;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class ProcessInstanceImageTag extends ProcessTagSupport {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -6934659542552737350L;

	private String alt;
	private String border;
	private String width;
	private String height;

	/**
	 * @see org.springframework.web.servlet.tags.RequestContextAwareTag#doStartTagInternal()
	 */
	protected int doStartTagInternal() throws Exception {
		ISystemProcessInstanceService systemProcessInstanceService = (ISystemProcessInstanceService) ProcessSystem
				.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_PROCESSINSTANCE_SERVICE);
		StringBuffer html = new StringBuffer();
		super.processInstanceId = super
				.getAttribute(EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_ID);
		if (super.processDefinitionId == null
				|| super.processDefinitionId.trim().length() == 0) {
			super.processDefinitionId = super
					.getAttribute(EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_ID);
		}
		if (super.processDefinitionId == null
				|| super.processDefinitionId.trim().length() == 0) {
			try {
				super.processDefinitionId = systemProcessInstanceService
						.getDetailById(super.processInstanceId)
						.getProcessDefinitionId();
			} catch (ObjectNotFoundException e) {
				ISystemHistoryProcessInstanceService systemHistoryProcessInstanceService = (ISystemHistoryProcessInstanceService) ProcessSystem
						.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_HISTORYPROCESSINSTANCE_SERVICE);
				super.processDefinitionId = systemHistoryProcessInstanceService
						.getDetailById(super.processInstanceId)
						.getProcessDefinitionId();
			}
		}
		html
				.append("<img alt=\""
						+ this.alt
						+ "\" src=\"process/processDefinitionAction!doGetDefinitionImage.action?processInformation.processDefinitionId="
						+ super.processDefinitionId + "\"");
		super.appendAttribute(html, "width", this.width, null);
		super.appendAttribute(html, "height", this.height, null);
		super.appendAttribute(html, "border", this.border, null);
		html.append(">\n");
		Set<ActivityCoordinates> activityCoordinatesSet = null;
		try {
			activityCoordinatesSet = systemProcessInstanceService
					.getActivityCoordinates(super.processInstanceId);
			for (ActivityCoordinates a : activityCoordinatesSet) {
				html
						.append("<div style=\"width:"
								+ a.getWidth()
								+ "px; height:"
								+ a.getHeight()
								+ "px; border:4px solid #FF0000; position:absolute; left: "
								+ a.getX() + "px; top: " + a.getY()
								+ "px;\"></div>\n");
			}
		} catch (ObjectNotFoundException e) {
		}
		JspWriter out = super.getOut();
		out.write(html.toString());
		return SKIP_BODY;
	}

	/**
	 * @return the alt
	 */
	public String getAlt() {
		return alt;
	}

	/**
	 * @param alt
	 *            the alt to set
	 */
	public void setAlt(String alt) {
		this.alt = alt;
	}

	/**
	 * @return the border
	 */
	public String getBorder() {
		return border;
	}

	/**
	 * @param border
	 *            the border to set
	 */
	public void setBorder(String border) {
		this.border = border;
	}

	/**
	 * @return the width
	 */
	public String getWidth() {
		return width;
	}

	/**
	 * @param width
	 *            the width to set
	 */
	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public String getHeight() {
		return height;
	}

	/**
	 * @param height
	 *            the height to set
	 */
	public void setHeight(String height) {
		this.height = height;
	}

}
